<?php

class OrderRepository {

    var $db;

    function OrderRepository() {
        global $SERVER, $DATABASE, $USERNAME, $PASSWORD;
        $this->db = new Connection($SERVER, $DATABASE, $USERNAME, $PASSWORD);
    }

    public function CreateOrder(OrderEntity $orderEntity) {
        $query = "INSERT INTO `order` (CustomerName_s,Email_s,Address_s,Phone_s,OrderTime_dt,DeliveryTime_dt,Checked_b)";
        $query.=" VALUES('" . $orderEntity->CustomerName_s . "', ";
        $query.="'" . $orderEntity->Email_s . "', ";
        $query.="'" . $orderEntity->Address_s . "', ";
        $query.="'" . $orderEntity->Phone_s . "', ";
        $query.="'" . $orderEntity->OrderTime_dt . "', ";
        $query.="'" . $orderEntity->DeliveryTime_dt . "', ";
        $query.= $orderEntity->Checked_b . ") ";

        if (isset($_SESSION['cart'])) {
            mysql_query('BEGIN');
            if (mysql_query($query) != 0) {
                $orderId = mysql_insert_id();
                foreach ($_SESSION['cart'] as $key => $value) {
                    $quantity = $value;
                    $productCode = $key;
                    $orderDetail = new OrderDetailEntity(0, $orderId, $productCode, $quantity);
                    if (!$this->CreateOrderDetail($orderDetail)) {
                        mysql_query("ROLLBACK");
                        return FALSE;
                    }
                }
                mysql_query("COMMIT");
                unset($_SESSION['cart']);
                return TRUE;
            } else {
                mysql_query("ROLLBACK");
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    public function CreateOrderDetail(OrderDetailEntity $orderDetail) {
        $query = "INSERT INTO OrderDetail (OrderID_i,ProductCode_s,Quantity_i)";
        $query.=" VALUES('$orderDetail->OrderID_i', ";
        $query.=" '$orderDetail->ProductCode_s', ";
        $query.=" '$orderDetail->Quantity_i') ";
        if (mysql_query($query) != 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //$currentPage : Trạng hiện hành
    //$pageSize : số item trên 1 trang
    //$totalRecords : tổng số item của bảng
    public function GetPager($currentPage, $itemsPerPage, &$totalRecords) {
        $startRecord = ($currentPage - 1) * $itemsPerPage;
        $query = "SELECT * FROM `order` LIMIT " . $startRecord . ", " . $itemsPerPage;
        $queryCount = "SELECT COUNT(*) as total FROM `order`";
        if ($result = mysql_fetch_array($this->db->ExecuteQuery($queryCount))) {
            $totalRecords = $result[0];
        }
        return $this->ConvertToList($this->db->ExecuteQuery($query));
    }

    public function GetOrderByDateOrder($dateBegin, $dateEnd, $currentPage, $itemsPerPage, &$totalRecords) {
        $startRecord = ($currentPage - 1) * $itemsPerPage;
        $query = "SELECT * FROM `order` WHERE OrderTime_dt BETWEEN '" . $dateBegin . "' AND '" . $dateEnd . "' LIMIT " . $startRecord . ", " . $itemsPerPage;
        $queryCount = "SELECT COUNT(*) as total FROM `order` WHERE OrderTime_dt BETWEEN '" . $dateBegin . "' AND '" . $dateEnd . "'";
        if ($result = mysql_fetch_array($this->db->ExecuteQuery($queryCount))) {
            $totalRecords = $result[0];
        }
        return $this->ConvertToList($this->db->ExecuteQuery($query));
    }

    public function GetOrderByDateDelivery($dateBegin, $dateEnd, $currentPage, $itemsPerPage, &$totalRecords) {
        $startRecord = ($currentPage - 1) * $itemsPerPage;
        $query = "SELECT * FROM `order` WHERE DeliveryTime_dt BETWEEN '" . $dateBegin . "' AND '" . $dateEnd . "' LIMIT " . $startRecord . ", " . $itemsPerPage;
        $queryCount = "SELECT COUNT(*) as total FROM `order` WHERE OrderTime_dt BETWEEN '" . $dateBegin . "' AND '" . $dateEnd . "'";
        if ($result = mysql_fetch_array($this->db->ExecuteQuery($queryCount))) {
            $totalRecords = $result[0];
        }
        return $this->ConvertToList($this->db->ExecuteQuery($query));
    }

    public function GetOrderByCheck($check, $currentPage, $itemsPerPage, &$totalRecords) {
        $startRecord = ($currentPage - 1) * $itemsPerPage;
        $query = "SELECT * FROM `order` WHERE Checked_b = $check LIMIT " . $startRecord . ", " . $itemsPerPage;
        $queryCount = "SELECT COUNT(*) as total FROM `order` WHERE Checked_b = $check";
        if ($result = mysql_fetch_array($this->db->ExecuteQuery($queryCount))) {
            $totalRecords = $result[0];
        }
        return $this->ConvertToList($this->db->ExecuteQuery($query));
    }

    public function GetOrderByEmail($email, $currentPage, $itemsPerPage, &$totalRecords) {
        $startRecord = ($currentPage - 1) * $itemsPerPage;
        $query = "SELECT * FROM `order` WHERE Email_s LIKE '" . $email . "' LIMIT " . $startRecord . ", " . $itemsPerPage;
        $queryCount = "SELECT COUNT(*) as total FROM `order` WHERE Email_s LIKE '" . $email . "'";
        if ($result = mysql_fetch_array($this->db->ExecuteQuery($queryCount))) {
            $totalRecords = $result[0];
        }
        return $this->ConvertToList($this->db->ExecuteQuery($query));
    }

    public function GetOrderByPhone($phone, $currentPage, $itemsPerPage, &$totalRecords) {
        $startRecord = ($currentPage - 1) * $itemsPerPage;
        $query = "SELECT * FROM `order` WHERE Phone_s LIKE '" . $phone . "' LIMIT " . $startRecord . ", " . $itemsPerPage;
        $queryCount = "SELECT COUNT(*) as total FROM `order` WHERE Phone_s LIKE '" . $phone . "'";
        if ($result = mysql_fetch_array($this->db->ExecuteQuery($queryCount))) {
            $totalRecords = $result[0];
        }
        return $this->ConvertToList($this->db->ExecuteQuery($query));
    }

    public function ConvertToList($result) {
        $list = array();
        while ($row = mysql_fetch_array($result)) {
            $order = new OrderEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7]);
            $list[] = $order;
        }
        return $list;
    }

    public function DeleteOrder($id) {
        $query = "DELETE FROM `order` WHERE ID_i = '" . $id . "'";

        if ($this->db->ExecuteQuery($query) != 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function GetOrder($id) {
        $query = "SELECT * FROM `order` WHERE ID_i = '" . $id . "'";
        $result = $this->db->ExecuteQuery($query) or die("Query failed with error: " . mysql_error());
        if ($result) {
            $row = mysql_fetch_array($result, MYSQL_NUM);
            return new OrderEntity($row[0], $row[1], $row[2],
                    $result[3], $row[4], $row[5], $row[6], $row[7]);
        }
        return NULL;
    }

    public function GetOrderDetails($orderId) {
        $query = "SELECT * FROM `orderdetail` WHERE OrderID_i = '" . $orderId . "'";
        $result = $this->db->ExecuteQuery($query);
        $array = array();
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            $entity = new OrderDetailEntity($row[0], $row[1],
                            $row[2], $row[3]);
            $array[] = $entity;
        }
        return $array;
    }

    public function UpdateOrderChecked($orderId, $checked) {
        $query = "UPDATE `order` SET Checked_b = $checked WHERE ID_i=$orderId";
        if ($this->db->ExecuteQuery($query) != 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}

?>
